<?php

class Admin_GalleryController extends Zend_Controller_Action
{

    public function init()
    {
		$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/css/gallery.css');
		$this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery-1.8.1.min.js');
    }

    public function indexAction()
    {
        // action body
    }
    
    public function addNewAction(){
    	$form=new Application_Form_NewGallery();
    	$this->view->form=$form;
		if($this->getRequest()->isPost()){
			$formData=$this->getRequest()->getPost();
			if($form->isValid($formData)){
				$db_model=new Application_Model_DbTable_Gallery();
				$imgdata=$this->uploadImgs($form);
				$data=array(
					'gallery_title'=>$form->getValue('gallery_title'),
					'gallery_url_spell'=>$form->getValue('gallery_url_spell'),
					'gallery_key_words'=>$form->getValue('gallery_key_words'),
					'gallery_description'=>$form->getValue('gallery_description'),
				    'gallery_text_description'=>$form->getValue('text_description'),
					'gallery_logo'=>$imgdata[0],
					'gallery_img'=>serialize($imgdata),
					'gallery_buy'=>$form->getValue('gallery_buy'),
					'gallery_shop'=>$form->getValue('gallery_shop'),
					'gallery_shop_url'=>$form->getValue('gallery_shop_url'),
					'gallery_count_num'=>'0',
					'gallery_create_date'=>new Zend_Db_Expr('now()')
				);
				$db_model->addNewGallery($data);
			}
		}
    }
    
    private function uploadImgs($form){
		$month=(string)date('Ym',time());
		$daytime=(string)date('dHi',time());
		$path="./images/galleryimg/".$month."/".$daytime;
		if(!is_dir($path)){
			mkdir($path,0777,true);
		}
		$imgdata=array();
		$logo=$form->gallery_logo;
		$logo->setDestination($path);
	    $logoName=$form->getValue('gallery_logo');
	    $imgdata[]='http://www.taoxiuyu.cn/images/galleryimg/'.$month.'/'.$daytime.'/'.$logoName;
		
    	for($i=1;$i<=6;$i++){
    		$var="img".$i;
	    	$img=$form->$var;
	    	$img->setDestination($path);
	    	$imgname=$form->getValue($var);
	    	if(isset($imgname)){
	    		$imginfo=array('imgurl'=>'http://www.taoxiuyu.cn/images/galleryimg/'.$month.'/'.$daytime.'/'.$imgname,
	    		'alt'=>$form->getValue('alt'.$i));
	    		$imgdata[]=$imginfo;
	    	}
    	}
		
    	return $imgdata;
    }
    
    


}

